<?php

class __admin_system_types_connections extends Module {
	
	public function __construct () {
		parent::__construct('system');
	}
	
	public function types_connections () {
		$response = $this->getTypesConnections ();
		$response->setTemplate ('system/typesConnections/list');
		
		if (getRequest('created','bool')) {
			$response->addObject(new SystemMessage(lang('admin_connection_created','system')));
		}
		if (getRequest('saved','bool')) {
			$response->addObject(new SystemMessage(lang('admin_connection_saved','system')));
		}
		if (getRequest('deleted','bool')) {
			$response->addObject(new SystemMessage(lang('admin_connection_deleted','system')));
		}
		
		return $response;
	}
	
	public function add_types_connection () {
		return $this->formTypesConnection();
	}
	
	public function edit_types_connection () {
		return $this->formTypesConnection(getRequest('param0','int'));
	}
	
	public function delete_types_connection () {
		$this->expectSecureGET ();
		
		if (!$object = ObjectsController::getInstance()->getObject (TypesController::getInstance()->getTypeByName ('explay_types_connection'), getRequest('param0','int'))) {
			throw new NotFound;
		}
		
		$object->delete ();
		
		redirect (HTTP_HOST . '/admin/system/types_connections?deleted');
	}
	
	public function getTypesConnections ($filteredTypeId = false) {
		$typesController = TypesController::getInstance ();
		$fieldsController = FieldsController::getInstance ();
		
		$oType = $typesController->getTypeByName ('explay_types_connection');
		
		$sel = new Selection;
		$sel->addType ($oType);
		
		if ($filteredTypeId) {
			$sel->addFilter ('parent_type_id', $filteredTypeId);
			$sel->addFilter ('child_type_id', $filteredTypeId);
			$sel->setLogicOr ();
		}
		
		$sel->setOrder ('parent_type_id');
		
		$aObjects = ObjectsController::getInstance()->getObjectsList ($sel->getList ());
		
		$response = new ModuleResponse;
		
		foreach ($aObjects as $object) {
			$parentTypeTitle = $typesController->getTypeById($object->getValue ('parent_type_id'))->getTitle ();
			$childTypeTitle = $typesController->getTypeById($object->getValue ('child_type_id'))->getTitle ();
			
			$object->setAttribute ('parent-type-title', $parentTypeTitle);
			$object->setAttribute ('child-type-title', $childTypeTitle);
			
			$parentFieldId = $object->getValue ('parent_field_id');
			$parentFieldTitle = 'id';
			if ($parentFieldId) {
				$parentFieldTitle = $fieldsController->getField($parentFieldId)->getTitle ();
			}
			
			$childFieldTitle = $fieldsController->getField($object->getValue ('child_rel_field_id'))->getTitle ();
			
			$object->setAttribute ('parent-field-title', $parentFieldTitle);
			$object->setAttribute ('child-field-title', $childFieldTitle);
			
			$response->addObject ($object);
		}
		
		return $response;
	}
	
	public function ajax_type_fields_connection () {
		$typesController = TypesController::getInstance ();
		$objectsController = ObjectsController::getInstance ();
		
		$oTypeConnection = $typesController->getTypeByName ('explay_types_connection');
		$oTypeField = $typesController->getTypeByName ('explay_field');
		
		$connectionId = getRequest ('connection','int');
		$typeId = getRequest ('type','int');
		
		if (!$oConnection = $objectsController->getObject ($oTypeConnection, $connectionId)) {
			$this->flush ((string) new Array2XML ('fields', array ()), 'text/xml');
		}
		
		if ($oConnection->getValue ('parent_type_id') == $typeId) {
			$needTypeId = $oConnection->getValue ('child_type_id');
		} elseif ($oConnection->getValue ('child_type_id') == $typeId) {
			$needTypeId = $oConnection->getValue ('parent_type_id');
		} else {
			$this->flush ((string) new Array2XML ('fields', array ()), 'text/xml');
		}
		
		$sel = new Selection;
		$sel->addType ($oTypeField);
		$sel->addFilter ('type_id', $needTypeId);
		$sel->setOrder ('position');
		
		$aObjects = $objectsController->getObjectsList ($sel->getList ());
		
		$response = array ();
		
		foreach ($aObjects as $object) {
			$response[]['field'] = array (
				'id' => $object->getId (),
				'title' => $object->getValue ('title')
			);
		}
		
		$this->flush ((string) new Array2XML ('fields', $response), 'text/xml');
	}
	
	private function formTypesConnection($id = 'new') {
		$oTypeConnection = TypesController::getInstance()->getTypeByName ('explay_types_connection');
		
		$form = new Form($oTypeConnection, $id);
		$form->buildFromType();
		
		$aErrors = array();
		$aValues = array();
		
		if ($id != 'new') {
			if (!$object = ObjectsController::getInstance()->getObject($oTypeConnection, $id)) {
				throw new NotFound;
			}
			
			$aValues = $object->getValues();
		}
		
		if ((getRequest ('param0') == 'save' && $id == 'new') || getRequest ('param1') == 'save') {
			$aValues = $this->getRequestData ($id, $oTypeConnection);
			
			if ($id == 'new') {
				$object = new Object($oTypeConnection);
				$object->setValues($aValues);
				
				$aErrors = $this->getValidateTypesConnection($id, $object);
				
				if (count($aErrors) == 0) {
					$object->createNew();
					
					redirect(HTTP_HOST . '/admin/system/types_connections?created');
				}
			} else {
				$object->setValues($aValues);
				
				$aErrors = $this->getValidateTypesConnection($id, $object);
				
				if (count($aErrors) == 0) {
					$object->save();
					
					redirect(HTTP_HOST . '/admin/system/types_connections?saved');
				}
			}
		}
		
		$form->setErrors($aErrors);
		$form->setValues($aValues);
		
		$response = new ModuleResponse;
		$response->setTemplate('system/typesConnections/form');
		$response->addObject($form);
		
		return $response;
	}
	
	private function getValidateTypesConnection ($id, Object $object) {
		$typesController = TypesController::getInstance();
		$fieldsController = FieldsController::getInstance();
		
		$aErrors = array();
		
		$parentTypeId = $object->getValue('parent_type_id');
		$childTypeId = $object->getValue('child_type_id');
		
		$parentFieldId = $object->getValue('parent_field_id');
		$childFieldId = $object->getValue('child_rel_field_id');
		
		if (!$parentTypeId || !$oParentType = $typesController->getTypeById($parentTypeId)) {
			$aErrors['parent_type_id'] = lang('error_admin_connections_wrong_parent_type','system');
		}
		
		if (!$childTypeId || !$oChildType = $typesController->getTypeById($childTypeId)) {
			$aErrors['child_type_id'] = lang('error_admin_connections_wrong_child_type','system');
		}
		
		if (!$childFieldId || !$oChildField = $fieldsController->getField($childFieldId)) {
			$aErrors['child_rel_field_id'] = lang('error_admin_connections_wrong_child_field','system');
		}
		
		if ($parentFieldId && !$oParentField = $fieldsController->getField($parentFieldId)) {
			$aErrors['parent_field_id'] = lang('error_admin_connections_wrong_parent_field','system');
		}
		
		if (count($aErrors)) {
			return $aErrors;
		}
		
		$sel = new Selection;
		$sel->addType($typesController->getTypeByName('explay_types_connection'));
		$sel->addFilter('parent_type_id', $parentTypeId);
		$sel->addFilter('child_type_id', $childTypeId);
		$sel->addFilter('parent_field_id', $parentFieldId);
		$sel->addFilter('child_rel_field_id', $childFieldId);
		if ($id != 'new') {
			$sel->addFilter('id', $id, '!=');
		}
		
		if ($sel->getTotalCount() != 0) {
			return array('parent_type_id' => lang('error_admin_connections_already_exists','system'));
		}
		
		return array();
	}
	
}